var ajaxUrl = require('../../../utils/url.js')
var encrypt = require('../../../utils/encrypt.js')
Page({
  data: {
    address: {
      addressId: 0,
      cityCode: "",
      cityName: "",
      contactNumber: "",
      countyCode: "",
      countyName:"",
      defaultAddressFlag:true,
      name:"",
      provinceCode:"",
      provinceName:"",
      streetCode:"",
      streetName:"",
      userId:3
    },
    goodsInfo:[],
    trackingType: 1,
    buyerRemark: '',
    freight: 0,
    count: 0,
    amount: 0,
    total: 0,
    transport: [
      {label: '物流',value: 1},
      {label: '空运',value: 2}
    ],
    index: 0,
  },
  onShow () {
    const self = this
    wx.getStorage({
      key: 'defaultAddress',
      success: function(res) {
        if(res.data){
          self.setData({
            address:res.data
          })
          self.getFreight()
        }
      } 
    })
  },
  onLoad (option) {
    const self = this
    if (option.id) {
      this.goodsId = option.id
      wx.getStorage({
        key: 'goodsInfo',
        success: function(res) {
          let goodsInfo = res.data
          goodsInfo[0].cartCnt = 1
          self.setData({
            goodsInfo: goodsInfo
          })
          self.setCount()
        } 
      })
    }else{
      wx.getStorage({
        key: 'goodsInfo',
        success: function(res) {
          let goodsInfo = res.data
          self.setData({
            goodsInfo: goodsInfo
          })
          self.setCount()
        } 
      })
    }
  },
  setCount () {
    const self = this
    let count = 0
    self.data.goodsInfo.forEach(item => {
      count += item.cartCnt

      self.setData({
        count: count
      })
    })
  },
  changeBuyerRemark (e) {
    const self = this
    self.setData({
      buyerRemark: e.detail.value
    })
  },
  getFreight () {
    const self = this
    self.setData({
      trackingType: self.data.transport[self.data.index].value
    })
    let params = {
      addressId:self.data.address.addressId,
      trackingType:self.data.trackingType,
      orderItemList:[]
    }
    self.data.goodsInfo.forEach((item) => {
      params.orderItemList.push({goodsId: item.goodsId, cnt: item.cartCnt})
    })
    console.log(params)
    params = encrypt.encrypt(params)
    wx.request({
      method: "POST",
      url: ajaxUrl.getFreight(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          self.setData({
            amount: res.data.data.totalAmount -  res.data.data.totalFreight,
            freight: res.data.data.totalFreight,
            total: res.data.data.totalAmount
          })
        }else{
          // console.error(res.data.msg)
          wx.showModal({
            content: res.data.msg+'请重新下单',
            showCancel: false,
            confirmText: "确定",
            success: function(res){
              if (res.confirm) {
                console.log(res)
                // wx.navigateBack({
                //   delta: 2
                // })
              }
            }
          })
        }
      }
    })
  },
  submitOrder () {
    const self = this
    let params = {
      addressId: self.data.address.addressId,
      buyerRemark: self.data.buyerRemark,
      trackingType:self.data.trackingType,
      freight: self.data.freight,
      orderItemList: [],
      totalAmount: parseInt(self.data.total)
    }
    self.data.goodsInfo.forEach((item, i) => {
      params.orderItemList.push({cnt: item.cartCnt, goodsId: item.goodsId})
    })
    params = encrypt.encrypt(params)
    wx.request({
      method: "POST",
      url: ajaxUrl.addOrder(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          wx.setStorage({
            key:"totalAmount",
            data: self.data.total
          })
          wx.navigateTo({
            url: '../pay/pay'
          });
        }
      }
    })
  },
  bindPickerChange: function(e) {
    const self = this
    this.setData({
      index: e.detail.value
    })
    self.getFreight()
  }
})